package com.jinwei.qunguanjia.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.common.constant.UserConstants;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.*;
import com.jinwei.qunguanjia.user.dao.GroupCommentDAO;
import com.jinwei.qunguanjia.user.dto.GroupCommentDTO;
import com.jinwei.qunguanjia.user.po.GroupCommentPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("groupCommentService")
public class GroupCommentServiceImpl implements GroupCommentService {

    @Resource
    private GroupCommentDAO groupCommentDAO;

    @Override
    public String save(GroupCommentDTO groupCommentDTO) {
        GroupCommentPO groupCommentPO = new GroupCommentPO();
        BeanUtils.copyProperties(groupCommentDTO, groupCommentPO);
        String groupCommentId = IdUtils.getWorkerId();
        groupCommentPO.setGroupCommentId(groupCommentId);
        groupCommentPO.setCreateTime(DateUtils.getDateTime());
        groupCommentDAO.insert(groupCommentPO);
        return groupCommentId;
    }

    @Override
    public void remove(String id) {
        groupCommentDAO.deleteById(id);
    }

    @Override
    public void update(GroupCommentDTO groupCommentDTO) {
        GroupCommentPO groupCommentPO = new GroupCommentPO();
        BeanUtils.copyProperties(groupCommentDTO, groupCommentPO);
        groupCommentDAO.updateById(groupCommentPO);
    }

    @Override
    public GroupCommentDTO get(String id) {
        GroupCommentPO groupCommentPO = groupCommentDAO.selectById(id);
        if (null == groupCommentPO) {
            return null;
        }
        GroupCommentDTO groupCommentDTO = new GroupCommentDTO();
        BeanUtils.copyProperties(groupCommentPO, groupCommentDTO);
        return groupCommentDTO;
    }

    @Override
    public GroupCommentDTO get(GroupCommentDTO groupCommentDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(groupCommentDTO);
        // 拼接查询条件
        QueryWrapper<GroupCommentPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        GroupCommentPO groupCommentPO = groupCommentDAO.selectOne(queryWrapper);
        if (null == groupCommentPO) {
            return null;
        }
        BeanUtils.copyProperties(groupCommentPO, groupCommentDTO);
        return groupCommentDTO;
    }

    @Override
    public Long count(GroupCommentDTO groupCommentDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(groupCommentDTO);
        // 拼接查询条件
        QueryWrapper<GroupCommentPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return groupCommentDAO.selectCount(queryWrapper);
    }

    @Override
    public List<GroupCommentDTO> list(GroupCommentDTO groupCommentDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(groupCommentDTO);
        // 拼接查询条件
        QueryWrapper<GroupCommentPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return JsonUtils.copyList(groupCommentDAO.selectList(queryWrapper), GroupCommentDTO.class);
    }

    @Override
    public PageDataInfo<GroupCommentDTO> listPage(GroupCommentDTO groupCommentDTO) {
        List<GroupCommentDTO> list = groupCommentDAO.list(groupCommentDTO);
        PageInfo<GroupCommentDTO> pageInfo = new PageInfo<>(list);
        PageDataInfo<GroupCommentDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(list);
        pageDataInfo.setTotal(pageInfo.getTotal());
        pageDataInfo.setTotalPage(PageCountUtils.getPageCount(pageInfo.getTotal(), pageInfo.getPageSize()));
        return pageDataInfo;
    }

    @Override
    public String checkUnique(GroupCommentDTO groupCommentDTO) throws IllegalAccessException {
        String id = StringUtils.isNull(groupCommentDTO.getGroupCommentId()) ? "-1" : groupCommentDTO.getGroupCommentId();
        GroupCommentDTO info = get(groupCommentDTO);
        if (StringUtils.isNotNull(info) && !info.getGroupId().equals(id)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    
}
